var v_index = new Vue({
	el: '#sharesDiv',
	data: {
		rows: [],
		showAdd: false,
		bgs: [
			'bg-primary',
			'bg-secondary',
			'bg-success',
			'bg-danger',
			'bg-warning',
			'bg-info',
			'bg-dark'
		],
		form: {
			id: '',
			market: 'sh',
			code: '',
			buyTime: ''
		},
		alert: {
			title: '提示',
			content: '内容'
		}
	},
	created() {
		this.load();
		jQuery.datetimepicker.setLocale('zh');
	},
	methods: {
		load() {
			this.initShares();
			this.initSharesAlert();
		},
		handleAdd() {
			this.showAdd = true;
			this.form = {
				id: '',
				market: 'sh',
				code: '',
				buyTime: ''
			}
			$('#sharesBuyTimeInput').datetimepicker({
				timepicker:false,
				format:'Y-m-d',
				onSelectDate:function(ct,$i){
					v_index.form.buyTime = $i.val();
				}
			});
		},
		handleEdit(row) {
			this.showAdd = true;
			$('#sharesBuyTimeInput').datetimepicker({
				timepicker:false,
				format:'Y-m-d',
				onSelectDate:function(ct,$i){
					v_index.form.buyTime = $i.val();
				}
			});
			this.form.id = row.shares.id;
			this.form.market = row.shares.market;
			this.form.code = row.shares.code;
			this.form.buyTime = row.shares.buyTime;
		},
		handleSave() {
			if (this.form.id) {
				this.editShares();
			} else {
				this.addShares();
			}
		},
		handleHide(row) {
			row.isShow = false;
			this.$forceUpdate();
		},
		handleDelete(row) {
			var result = confirm('是否删除当前的股票信息？删除后将删除所有的统计记录，并且不能恢复。');
			if (result) {
				this.deleteShares(row.shares.id);
			}
		},
		handleLoadMore(row) {
			if (!row.showDetails) {
				row.showDetails = true;
			} else {
				this.loadShares(row.page + 1, row.shares.id);
			}
			this.$forceUpdate();
		},
		initShares() {
			$.post('/shares/daily/page', (resp) => {
				if (resp.success) {
					this.rows = resp.rows;
					$.each(this.rows, (index, row) => {
						row.bg = v_index.bgs[index % 7];
						row.isShow = true;
						row.showDetails = false;
					});
				} else {
					alert(resp.data);
				}
			});
		},
		initSharesAlert() {
			$.get('/shares/sale/alert/check', (resp) => {
				if (resp.success && resp.data.status) {
					var alertShares = $.map(resp.data.shareses, (shares) => {
						return shares.name + '（' + shares.code + '）建议卖出价：' + (shares.maxPrice * 0.9).toFixed(2);
					}).join('\r\n');
					var r = confirm('达到最高金额90%的股票如下：\r\n' + alertShares + '\r\n是否关闭股票卖出提醒？');
					if (r) {
						$.post('/shares/sale/alert/close/' + resp.data.id);
					}
				}
			});
		},
		loadShares(page, sharesId) {
			$.post('/shares/daily/page', {page: (page ? page : 0), sharesId: (sharesId ? sharesId : '')}, (resp) => {
				if (resp.success) {
					$.each(this.rows, (index, row) => {
						if (row.shares.id == sharesId) {
							row.page = resp.rows[0].page;
							$.each(resp.rows[0].dailies, (i, daily) => {
								row.dailies.push(daily);
							});
						}
					});
				} else {
					alert(resp.data)
				}
			});
		},
		addShares() {
			$.post({
				url: '/shares/add',
			    contentType:'application/json; charset=UTF-8',
			    data: JSON.stringify(this.form),// 数据 必须是字符串
			    success: function(resp) {
			    	if (resp.success) {
			    		alert(resp.data);
			    		v_index.showAdd = false;
			    		$('#addSharesModal').modal('hide');
			    		v_index.initShares();
					} else {
						alert(resp.data);
					}
			    },
			    error: function(resp) {
			    	alert(resp.message);
			    }
			});
		},
		editShares() {
			$.ajax({
				url: '/shares/edit',
				method: 'put',
			    contentType:'application/json; charset=UTF-8',
			    data: JSON.stringify(this.form),// 数据 必须是字符串
			    success: function(resp) {
			    	if (resp.success) {
			    		alert(resp.data);
				    	v_index.showAdd = false;
				    	$('#addSharesModal').modal('hide');
				    	v_index.initShares();
					} else {
						alert(resp.data);
					}
			    },
			    error: function(resp) {
			    	alert(resp.message);
			    }
			});
		},
		deleteShares(id) {
			$.ajax({
				url: '/shares/delete/' + id,
				method: 'delete',
			    contentType:'application/json; charset=UTF-8',
			    data: JSON.stringify(this.form),// 数据 必须是字符串
			    success: function(resp) {
			    	alert(resp.data);
			    	v_index.initShares();
			    },
			    error: function(resp) {
			    	alert(resp.message);
			    }
			});
		}
	}
});
